package com.sportizz.sport.row;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.sportizz.sport.R;
import com.sportizz.sport.follow_player;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.PorterDuff.Mode;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

@SuppressLint("DefaultLocale")
public class Custom_set_row extends BaseAdapter {

	Activity act;
	LayoutInflater lif;
	int resID;
	List<follow_player> follow_list;

	public Custom_set_row(Context context, int resID, List<follow_player> obj) {
		act = (Activity) context;
		lif = (LayoutInflater) act
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.resID = resID;
		this.follow_list = obj;
	}

	@SuppressLint("DefaultLocale")
	@Override
	public View getView(int position, View v, ViewGroup parent) {
		ViewHolder viewholder;

		if (v == null) {
			viewholder = new ViewHolder();
			v = lif.inflate(resID, null);
			v.setOnClickListener(null);
			v.setOnLongClickListener(null);
			v.setLongClickable(false);
			viewholder.p_img = (ImageView) v.findViewById(R.id.set_p_img);
			viewholder.p_name = (TextView) v.findViewById(R.id.set_p_name);
			viewholder.p_sport = (TextView) v.findViewById(R.id.txt_sett_sport);
			viewholder.txt_swipe = (TextView) v.findViewById(R.id.txt_swipe);

			v.setTag(viewholder);
		} else {// convertview is not null
			viewholder = (ViewHolder) v.getTag();
		}

		Bitmap output;
		Canvas canvas;
		float roundPx;

		Log.v("suraj", "" + follow_list.get(position).getBitmap());

		if (follow_list.get(position).getBitmap() == null) {

			Log.i("suraj", "NULL BITMAP");
			Bitmap bYN=null;
			
			InputStream is = null;
			try {
				is = act.getResources()
						.getAssets()
						.open("new_sports/"
								+ follow_list.get(position).getsport_code()
								+ ".png");
				
				 bYN= BitmapFactory.decodeStream(is);
				
			} catch (Exception e2) {
				try {
					is = act.getResources()
							.getAssets()
							.open("new_sports/GAM.png");
				
				 bYN= BitmapFactory.decodeStream(is);
				} catch (IOException e) {
				}
			}

			output = Bitmap.createBitmap(bYN.getWidth(), bYN.getHeight(),
					Bitmap.Config.ARGB_8888);

			canvas = new Canvas(output);

			Paint paint = new Paint();
			Rect rect = new Rect(0, 0, bYN.getWidth(), bYN.getHeight());
			RectF rectF = new RectF(rect);
			roundPx = 8f;

			paint.setAntiAlias(true);
			canvas.drawARGB(0, 0, 0, 0);
			paint.setColor(0xff424242);
			canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(bYN, rect, rect, paint);

			viewholder.p_img.setImageBitmap(bYN);
			viewholder.p_img.setScaleType(ScaleType.FIT_CENTER);
		} 
		else 
		{

			output = Bitmap.createBitmap(follow_list.get(position).getBitmap()
					.getWidth(), follow_list.get(position).getBitmap()
					.getHeight(), Bitmap.Config.ARGB_8888);

			canvas = new Canvas(output);

			Paint paint = new Paint();
			Rect rect = new Rect(0, 0, follow_list.get(position).getBitmap()
					.getWidth(), follow_list.get(position).getBitmap()
					.getHeight());
			RectF rectF = new RectF(rect);
			roundPx = 8f;

			paint.setAntiAlias(true);
			canvas.drawARGB(0, 0, 0, 0);
			paint.setColor(0xff424242);
			canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(follow_list.get(position).getBitmap(), rect,
					rect, paint);

			viewholder.p_img.setImageBitmap(output);
			viewholder.p_img.setScaleType(ScaleType.FIT_CENTER);

		}

//		Typeface font_name = Typeface.createFromAsset(act.getAssets(),
//				"impact.ttf");
		
		Typeface sport_font = Typeface.createFromAsset(act.getAssets(),
				"hel57.ttf");

		viewholder.p_name.setText(follow_list.get(position).getName()
				.toUpperCase());
		viewholder.p_name.setTypeface(sport_font);

		viewholder.p_name.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

		viewholder.p_sport.setText(follow_list.get(position).getsport()
				.toUpperCase());
		viewholder.p_sport.setTypeface(sport_font);
		viewholder.p_sport
				.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

		viewholder.txt_swipe.setVisibility(View.VISIBLE);
		v.setClickable(false);

		return v;

	}

	public static class ViewHolder {
		
		public ImageView p_img;
		public TextView p_name, p_sport;
		public TextView txt_swipe;
	}

	@Override
	public int getCount() {
		return follow_list.size();
	}

	@Override
	public Object getItem(int location) {
		return follow_list.get(location);
	}

	@Override
	public long getItemId(int location) {
		return location;
	}
}